//
//  imageV-fast.swift
//  jocool
//
//  Created by tong on 16/6/7.
//  Copyright © 2016年 zhuxietong. All rights reserved.
//

import Foundation

import Foundation


public enum ImgHolderType{
    case none
    case head
    case `default`
    case grayBack
    case colorHex(String)
    case noAnimationType
    case image (UIImage?)
    
    
}

public protocol IMGSrc {
    var src:Any{get}
}

extension UIImage:IMGSrc{
    public var src: Any{
        get{
            return self
        }
    }
}
extension String:IMGSrc{
    public var src: Any{
        get{
            return self
        }
    }
}

extension UIImage{
    public func icon(_ width:CGFloat?=nil,_ height:CGFloat?=nil,insets:UIEdgeInsets=UIEdgeInsets.zero) ->UIImage{
        var __width = width ?? size.width
        var __height = height ?? size.height
        
        let rate = self.size.width / self.size.height
        
        if height == nil{
            if width != nil{
                __height = __width/rate
            }
        }
        if width == nil{
            if height != nil{
                __width = __height*rate
            }
        }
        
        let new_size:CGSize = CGSize(width: __width, height: __height)
        let imgV = UIImageView()
        imgV.backgroundColor = .clear
        imgV.image = self
        imgV.frame = CGRect(x: insets.left, y:insets.top , width: new_size.width, height: new_size.height)
        
        let contentV = UIView()
        contentV.frame = CGRect(x: 0, y:0 , width: (new_size.width + insets.left + insets.right), height: (new_size.height + insets.top + insets.bottom))
        contentV.addSubview(imgV)
        
        
        UIGraphicsBeginImageContextWithOptions(contentV.frame.size, false, 1.0*UIScreen.main.scale)
        contentV.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext();
        return image!
    }
    
}


extension UIImageView
{
    //    private struct AssociatedKeys {
    //        static var ImgholderTypeKey = "holderType_key"
    //    }
    //
    //    public var holderType: ImgHolderType {
    //        get {
    //            if let str = objc_getAssociatedObject(self, &AssociatedKeys.ImgholderTypeKey) as? String
    //            {
    //                if let t = ImgHolderType(rawValue: str)
    //                {
    //                    return t
    //                }
    //            }
    //            return .default
    //        }
    //        set {
    //            objc_setAssociatedObject(self, &AssociatedKeys.ImgholderTypeKey, newValue.rawValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
    //        }
    //    }
    
    public var name:String{
        get{
            return ""
        }
        set{
            self.image = UIImage(named: newValue)
        }
    }
}

public class ImageViewStyle:BaseStyle<UIImageView> {
    
    var holder:ImgHolderType = .default
    var circle:ImageViewStyle{
        self.owner?.__style.setObject("YES", forKey: "circle" as NSCopying)
        return self
    }
    var _scheme:UIImageView.URLSourceScheme = .unkown
    @discardableResult
    public func scheme(scheme:UIImageView.URLSourceScheme) ->ImageViewStyle{
        self._scheme = scheme
        return self
    }
    
    var contentMode: UIView.ContentMode  = .scaleAspectFill// default is UIViewContentModeScaleToFill
    @discardableResult
    public func holder(type:ImgHolderType) ->ImageViewStyle{
        self.holder = type
        return self
    }
    
    @discardableResult
    public func content(mode:UIView.ContentMode) ->ImageViewStyle{
        self.contentMode = mode
        return self
    }
    
    @discardableResult
    public func media_width(_ width:CGFloat) ->ImageViewStyle{
        self.owner?.media_width = width
        return self
    }
    
}

extension UIImageView{
    
    public var ui:ImageViewStyle{
        get{
            if let style = __style["ui"] as? ImageViewStyle{
                return style
            }
            let style = ImageViewStyle()
            style.owner = self
            __style["ui"] = style
            return style
        }
        set{
            __style["ui"] = newValue
            newValue.owner = self
        }
    }
}
extension UIImage{
    public static func gradient(colors:[UIColor],size:CGSize)->UIImage{
        let layerView = UIView()
        layerView.frame = CGRect(x: 0, y: 0, width: 347, height: 50)
        // layerFillCode
        
        let gradient = CAGradientLayer()
        gradient.colors = [colors[0].cgColor,colors[1].cgColor]
        gradient.locations = [0, 1]
        gradient.frame = layerView.bounds
        gradient.startPoint = CGPoint(x: 0, y: 0)
        gradient.endPoint = CGPoint(x: 1, y: 1)
        layerView.layer.addSublayer(gradient)
        layerView.layer.cornerRadius = 25;
        return layerView.shootImage
    }
}

extension UIImage {
    public func tinted(with color: UIColor,mode:RenderingMode = RenderingMode.alwaysTemplate) -> UIImage? {
     UIGraphicsBeginImageContextWithOptions(size, false, scale)
     defer { UIGraphicsEndImageContext() }
     color.set()
     withRenderingMode(mode)
      .draw(in: CGRect(origin: .zero, size: size))
     return UIGraphicsGetImageFromCurrentImageContext()
    }
}
extension UIImage{
    
    
    
    public static func dynamic(light:UIImage?,dark:UIImage?)->UIImage?{
        if #available(iOS 12.0, *) {
            if let d = dark{
                light?.imageAsset?.register(d, with: UITraitCollection(userInterfaceStyle: .dark))
            }
            
            if let l = light{
                light?.imageAsset?.register(l, with: UITraitCollection(userInterfaceStyle: .light))
            }
            
            if #available(iOS 13.0, *) {
                switch UITraitCollection.current.userInterfaceStyle {
                case .dark:
                    return dark
                default:
                    return light
                }
            } else {
                return light
            }

//            return light
        }
        return light ?? dark
    }
    
    public func dynamicImage(color:UIColor)->UIImage{
//        if #available(iOS 13.0, *) {
//            let l = color.resolvedColor(with: UITraitCollection(userInterfaceStyle: .light))
//            let d = color.resolvedColor(with: UITraitCollection(userInterfaceStyle: .dark))
//            let imgL = self.copy() as? UIImage
//            let imgD = self.copy() as? UIImage
//
//
//            return UIImage.dynamic(light: imgL?.rt_tintedImage(with: l), dark: imgD?.rt_tintedImage(with: d)) ?? self
//        } else {
            return self.rt_tintedImage(with: color)
//        }
//        return self
    }
    
    
    public static func dynamic(color:UIColor,size:CGSize=CGSize(width: 1, height: 1))->UIImage?{
        if #available(iOS 13.0, *) {
            let l = color.resolvedColor(with: UITraitCollection(userInterfaceStyle: .light))
            let d = color.resolvedColor(with: UITraitCollection(userInterfaceStyle: .dark))
            return UIImage.dynamic(light: UIImage.image(l, size: size), dark: UIImage.image(d, size: size))
        } else {
            // Fallback on earlier versions
        }
        return UIImage.image(color,size: size)
    }
    
}

